package org.csu.mypetstore_ssm.controller;

import org.csu.mypetstore_ssm.domain.Category;
import org.csu.mypetstore_ssm.domain.Item;
import org.csu.mypetstore_ssm.domain.Product;
import org.csu.mypetstore_ssm.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
//@RequestMapping("*/")
public class CatalogController {
    @Autowired
    CatalogService catalogService;

    //打开主页面
    @GetMapping(value = {"/main", "/mainForm"})
    public String main() {
        return "catalog/main";
    }

    @GetMapping("/category")
    public String category(@RequestParam("categoryId") String categoryId,Model model){

        Category category = catalogService.getCategory(categoryId);
        //System.out.println(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        model.addAttribute("category",category);
        model.addAttribute("productList",productList);
        return "catalog/category";
    }

    @GetMapping("/itemForm")
    public String item(@RequestParam("itemId") String itemId,Model model){
        Item item = catalogService.getItem(itemId);
        Product product = item.getProduct();

        model.addAttribute("product",product);
        model.addAttribute("item",item);
        model.addAttribute("add",itemId);
        return "catalog/item";
    }

    @GetMapping("/productForm")
    public String product(@RequestParam("productId") String productId,Model model){
        Product product = catalogService.getProduct(productId);
        List<Item> itemList = catalogService.getItemListByProduct(productId);
        model.addAttribute("product",product);
        model.addAttribute("itemList",itemList);
        return "catalog/product";
    }

}
